package com.maxd.controller.message;


import com.maxd.adapter.IgnoreSecurity;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.MessageInfo;
import com.maxd.respository.message.MessageRepository;
import com.maxd.service.message.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(value = "公告管理", tags = {"公告管理"})

@RequestMapping(value = "/message")
public class MessageController {
    @Autowired
    private IMessageService iMessageService;
    @Autowired
    private MessageRepository messageRepository;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiOperation("管理平台公告详情")
    @ResponseBody
    public Result getMessage(@PathVariable Integer id) {
        return iMessageService.findOne(Long.valueOf(id));
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("管理平台和用户端通用接口添加公告")
    @IgnoreSecurity
    @ResponseBody
    public Result addMessage(@RequestBody MessageInfo messageInfo) {
        return iMessageService.saveBody(messageInfo);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("管理平台修改公告接口")
    @ResponseBody
    public Result uUpdate(@RequestBody MessageInfo messageInfo) {
        return iMessageService.saveBody(messageInfo);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ApiOperation("管理平台删除公告接口")
    public Result deleteMessage(@PathVariable int id) {
        return iMessageService.delete(id);
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ApiOperation("管理平台获取全部公告接口")
    @ResponseBody
    public Result getMessageList() {
        return iMessageService.findAll();
    }

    @RequestMapping(value = "/page/{state}/{page}/{size}", method = RequestMethod.GET)
    @IgnoreSecurity
    @ApiOperation("管理平台分页查询公告接口")
    @ResponseBody
    public Result getBodyPage(@PathVariable String state, @PathVariable Integer page, @PathVariable int size) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return iMessageService.find(state, pageable);
    }

    @RequestMapping(value = "/type/{type}/{page}/{size}", method = RequestMethod.GET)
    @ApiOperation("管理平台通过类型获取接口 type1为公告2位用户反馈 3为系统消息 4为订单信息 5为用户消息 6客服消息")
    @ResponseBody
    public Result findType(@PathVariable Integer type, @PathVariable Integer page, @PathVariable int size) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return iMessageService.findType(type, pageable);
    }

    @RequestMapping(value = "/findType/{userId}/{type}/{page}/{size}", method = RequestMethod.GET)
    @ApiOperation("用户端获取消息列表 type 4为订单信息 5为用户消息")
    @ResponseBody
    public Result findType(@PathVariable String userId, @PathVariable String type, @PathVariable Integer page, @PathVariable int size) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(messageRepository.findTypeByUserId(type, userId, pageable));
    }


}
